package com.yangzc.lion.sys.security.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.yangzc.lion.sys.organization.model.SysAuthRole;
import com.yangzc.lion.sys.organization.model.SysOrgPerson;
import com.yangzc.lion.sys.organization.service.ISysAuthRoleService;
import com.yangzc.lion.sys.organization.service.ISysOrgPersonService;
import com.yangzc.lion.sys.security.model.SecurityUser;

/**
 * SPRING SECURITY3用户登录处理
 * 
 */
@Component("userDetailsService")
public class UserDetailsServiceImpl implements IUserDetailsService {


	private ISysOrgPersonService sysOrgPersonService;

	public ISysOrgPersonService getSysOrgPersonService() {
		return sysOrgPersonService;
	}

	@Resource(name = "sysOrgPersonService")
	public void setSysOrgPersonService(ISysOrgPersonService sysOrgPersonService) {
		this.sysOrgPersonService = sysOrgPersonService;
	}
	
	private ISysAuthRoleService sysAuthRoleService;

	public ISysAuthRoleService getSysAuthRoleService() {
		return sysAuthRoleService;
	}

	@Resource(name = "sysAuthRoleService")
	public void setSysAuthRoleService(ISysAuthRoleService sysAuthRoleService) {
		this.sysAuthRoleService = sysAuthRoleService;
	}

	private SecurityUser buildSecurityUser(SysOrgPerson person) {
		List<SysAuthRole> roles = getRoleBySysOrgPerson(person);
		List<GrantedAuthority> gaRoles = new ArrayList<GrantedAuthority>();

		for (SysAuthRole role : roles) {
			gaRoles.add(new SimpleGrantedAuthority(role.getFdAlias()));
		}
		if (gaRoles == null || gaRoles.isEmpty())
			throw new UsernameNotFoundException("权限不足!");
		SecurityUser user = new SecurityUser(person, gaRoles);
		// 获取首个用户名
		return user;
	}

	private List<SysAuthRole> getRoleBySysOrgPerson(SysOrgPerson person) {
		List<SysAuthRole> roles = new ArrayList<SysAuthRole>();
		try {
			roles = sysAuthRoleService.getRoleList(person.getFdId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		SysAuthRole role = new SysAuthRole();
		role.setFdAlias("ROLE_USER_DEFAULT");
		roles.add(role);
		return roles;
	}

	public SecurityUser loadUserByUsername(String username)
			throws UsernameNotFoundException {
		SysOrgPerson person = this.sysOrgPersonService
				.findByLoginName(username);
		if (person == null)
			throw new UsernameNotFoundException("用户/密码错误,请重新输入!");
		return buildSecurityUser(person);
	}

	public SecurityUser loadUserByUserId(String userId)
			throws UsernameNotFoundException {
		SysOrgPerson person = this.sysOrgPersonService.findByPrimaryKey(userId);
		if (person == null)
			throw new UsernameNotFoundException("用户/密码错误,请重新输入!");

		return buildSecurityUser(person);
	}

	public SecurityUser loadUserByUserEmail(String email)
			throws UsernameNotFoundException {
		SysOrgPerson person = this.sysOrgPersonService.findByEmail(email);
		if (person == null)
			throw new UsernameNotFoundException("用户/密码错误,请重新输入!");
		return buildSecurityUser(person);
	}
}